﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Mvc;
using JinianNet.JNCms.Filters;
using JinianNet.JNCms.Models;

namespace JinianNet.JNCms.Controllers
{
    [ManagerFilter()]
    public class CategoryController : ControllerBase
    {
        [HttpPost]
        [ValidateInput(false)] 
        public ActionResult Edit(Category model)
        {
            if (model.ParentID > 0)
            {
                Category category = Data.DataAccess.CreateCategory().GetItem(model.ParentID);
                if (category == null)
                {
                    return Content(Config.GetTips("对不起，父栏目不存在",null, false));
                }
                else
                {
                    model.Depth = category.Depth + 1;

                }
            }
            else
            {
                model.Depth = 0;
            }

            Data.DataAccess.CreateCategory().Update(model);
            return Content(Config.GetTips("恭喜，栏目修改成功", Url.Action("List", "Category", new { item = model.ParentID }), true));
        }

        public ActionResult Edit(string item)
        {
            if (!string.IsNullOrEmpty(item))
            {
                JinianNet.JNCms.Data.ICategory dal = Data.DataAccess.CreateCategory();
                Category model = Data.DataAccess.CreateCategory().GetItem(item);
                if (model != null)
                {
                    if (model.ParentID > 0)
                    {
                        
                        Category category = dal.GetItem(model.ParentID);
                        if (category == null)
                        {
                            ViewBag.ParentName = category.Title;
                        }
                        else
                        {
                            ViewBag.ParentName = "根目录";

                        }
                    }
                    else
                    {
                        ViewBag.ParentName = "根目录";
                    }
                    return View(model);
                }
            }
            return PageNotFound();
        }

        [HttpPost]
        [ValidateInput(false)] 
        public ActionResult Insert(Category model)
        {
            if (string.IsNullOrEmpty(model.KeyName))
            {
                model.KeyName = string.Concat(JinianNet.Common.Random.RandAlphabet(1), JinianNet.Common.Random.RandTime(3));
            }
            else
            {
                if (!JinianNet.Common.Utils.IsNumberOrLetter(model.KeyName.Replace("-", "").Replace("_", ""), true))
                {
                    return Content(Config.GetTips("对不起，栏目标识只能为字母，数字与“-”或“_”的组合，且只能以字母开头！", null, false));
                }
            }
            model.CreateTime = DateTime.Now;
            if (model.ParentID > 0)
            {
                Category category = Data.DataAccess.CreateCategory().GetItem(model.ParentID);
                if (category == null)
                {
                    return Content(Config.GetTips("对不起，父栏目不存在！", null, false));
                }
                else
                {
                    model.Depth = category.Depth + 1;

                }
            }
            else
            {
                model.Depth = 0;
            }

            Data.DataAccess.CreateCategory().Insert(model);
            return Content(Config.GetTips("恭喜，栏目添加成功", Url.Action("List", "Category", new { item = model.ParentID }), true));
        }

        public ActionResult Insert(int item=0)
        {
            Category model = new Category();
            model.CreateTime = DateTime.Now;
            model.Count = 0;
            model.ParentID = item;
            model.KeyName = string.Concat(JinianNet.Common.Random.RandAlphabet(1), JinianNet.Common.Random.RandNumber(9));
            if (model.ParentID > 0)
            {
                Category category = Data.DataAccess.CreateCategory().GetItem(model.ParentID);
                if (category == null)
                {
                    return Content(Config.GetTips("对不起，父栏目不存在", null, false));
                }
                else
                {
                    model.TempLate = category.TempLate;
                    model.ChildTempLate = category.ChildTempLate;
                    ViewBag.ParentName = category.Title;

                }
            }
            else
            {
                model.TempLate = "Category";
                model.ChildTempLate = "Article";
                ViewBag.ParentName = "根目录";
            }
            return View("Edit", model);
        }

        public ActionResult Delete(string item)
        {
            Data.ICategory dal = Data.DataAccess.CreateCategory();
            if (item.IndexOf(',') < 0)
                dal.Delete(int.Parse(item));
            else
                foreach (string value in item.Split(','))
                    if (!string.IsNullOrEmpty(value))
                        dal.Delete(int.Parse(value));
            return Content(Config.GetTips("操作成功", null, true));
        }


        public ActionResult List(int item=0)
        {
            return View(Data.DataAccess.CreateCategory().GetList(item, false));
        }
    }
}
